package com.stormrage.edu.serv.ent.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.stormrage.constants.ErrCons;
import com.stormrage.edu.dao.ent.EntEnterpriseDao;
import com.stormrage.edu.serv.ent.EntEnterpriseScoreServ;
import com.stormrage.edu.serv.ent.EntEnterpriseServ;
import com.stormrage.entity.ent.EntEnterprise;
import com.stormrage.entity.ent.EntEnterpriseScore;
import com.stormrage.entity.ent.rep.EntEnterpriseRep;
import com.stormrage.entity.ent.req.EntEnterpriseReq;
import com.stormrage.entity.excel.EntEnterpriseExcel;
import com.stormrage.entity.rep.ListRep;
import com.stormragetech.core.exception.StormrageTechException;
import com.stormragetech.core.util.BeanUtil;
import com.stormragetech.core.util.ExcelUtil;

/**
 * @Title: EntEnterpriseServImpl.java
 * @Package com.stormrage.edu.serv.ent.impl
 * @Description: 公司 Service
 * @author www.stormragetech.com
 * @date 2016年4月13日
 * @version V1.0
 */
@Service
public class EntEnterpriseServImpl implements EntEnterpriseServ {

	private static final Logger logger = LoggerFactory.getLogger(EntEnterpriseServImpl.class);

	@Autowired
	private EntEnterpriseDao entEnterpriseDao;

	@Autowired
	private EntEnterpriseScoreServ entEnterpriseScoreServ;

	@Override
	public Integer create(final EntEnterprise arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 Create 】-【 STARTED 】");

		// ------------------ 验证公司名称是否重复 ------------------
		Boolean b = isExist(arg0.getEnterprise());
		if (b) {
			throw StormrageTechException.instance(ErrCons.SERV_ERR, "公司名称已存在", StormrageTechException.ERR_APP,
					"【 EntEnterpriseServ 】-【 Create 】");
		}

		// ------------------ 创建管理员 ------------------
		Integer index = entEnterpriseDao.create(arg0);

		EntEnterpriseScore entScore = EntEnterpriseScore.instance(index);
		entEnterpriseScoreServ.create(entScore);

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 Create 】- 结束运行 ");
		return index;

	}

	@Override
	public EntEnterprise queryById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 QueryByName 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		EntEnterprise entity = entEnterpriseDao.queryById(map);

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 QueryByName 】- 结束运行 ");
		return entity;

	}

	@Override
	public EntEnterprise queryByName(final String arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 QueryByName 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", arg0);

		EntEnterprise entity = entEnterpriseDao.queryByName(map);

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 QueryByName 】- 结束运行 ");
		return entity;

	}

	@Override
	public List<EntEnterprise> queryList() throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 queryList 】-【 STARTED 】");

		List<EntEnterprise> array = entEnterpriseDao.queryList();

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 queryList 】- 结束运行 ");
		return array;

	}

	@Override
	public ListRep queryList(final EntEnterpriseReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 QueryList 】-【 STARTED 】");

		Map<String, Object> map = initQuery(arg0);

		Integer count = entEnterpriseDao.queryCount(map);

		List<EntEnterprise> array = entEnterpriseDao.queryList(map);

		List<EntEnterpriseRep> list = new ArrayList<EntEnterpriseRep>();
		int seq = arg0.getPage().getStartPage() + 1;

		Iterator<EntEnterprise> it = array.iterator();
		while (it.hasNext()) {
			EntEnterprise ent = it.next();
			EntEnterpriseScore entScore = entEnterpriseScoreServ.queryByEntId(ent.getId());
			EntEnterpriseRep rep = EntEnterpriseRep.instance(ent, seq, entScore.getLocked());
			list.add(rep);
			seq++;
		}

		ListRep rep = ListRep.instance(count, list);

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 QueryList 】-【 COMPLETED 】");
		return rep;
	}

	@Override
	public void upload(final MultipartFile file) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 upload 】-【 STARTED 】");

		try {

			List<EntEnterpriseExcel> array = ExcelUtil.importExcel(file.getInputStream(), EntEnterpriseExcel.class);
			List<EntEnterprise> list = queryList();

			Iterator<EntEnterpriseExcel> it = array.iterator();
			while (it.hasNext()) {
				EntEnterpriseExcel excel = it.next();
				for (EntEnterprise ent : list) {
					if (ent.getEnterprise().equals(excel.getEnterprise()))
						it.remove();
				}
			}

			if (array.size() > 0) {
				for (EntEnterpriseExcel excel : array) {
					EntEnterprise entity = EntEnterprise.instance(excel.getEnterprise());
					create(entity);
				}
			}

		} catch (IOException e) {
			throw StormrageTechException.instance(ErrCons.SERV_EXP, ErrCons.SERV_EXP_MSG,
					StormrageTechException.ERR_SYS, "【 EduChapterServ 】- 【 upload 】", e);
		}

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 upload 】-【 COMPLETED 】");

	}

	private Boolean isExist(String arg0) throws StormrageTechException {

		Boolean rs = Boolean.TRUE;

		EntEnterprise entity = queryByName(arg0);
		if (null == entity)
			rs = Boolean.FALSE;

		return rs;

	}

	private Map<String, Object> initQuery(EntEnterpriseReq arg0) {

		if (null == arg0)
			return null;

		Map<String, Object> map = BeanUtil.initQuery(arg0.getPage());

		StringBuffer query = new StringBuffer("");

		if (null != arg0.getEnterprise() && arg0.getEnterprise().length() > 0) {
			query.append("enterprise LIKE :enterprise AND ");
			map.put("enterprise", "%" + arg0.getEnterprise() + "%");
		}

		String sql = query.toString();
		if (null != sql && sql.length() > 0)
			map.put("query", " WHERE " + sql.substring(0, sql.length() - 4));
		else
			map.put("query", "");

		return map;
	}

}
